package com.rxw.nutmall.oss.utils;


import com.rxw.nutmall.oss.entity.File;
import com.rxw.nutmall.utils.Result;
import com.rxw.nutmall.oss.config.NacosParameter;
import io.minio.*;
import io.minio.http.Method;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import java.io.InputStream;
import java.sql.Timestamp;

@Component
public class MinioUtil {

    private static final Logger log = LoggerFactory.getLogger(MinioUtil.class);

    private static NacosParameter parameter;
    @Autowired
    public void setParameter(NacosParameter parameter) {
        MinioUtil.parameter = parameter;
    }

    private static MinioClient minioClient;
    @Autowired
    public void setMinioClient(MinioClient minioClient) {
        MinioUtil.minioClient = minioClient;
    }

    public static Result<Boolean> existBucket(String name) {
        boolean exists;
        try {
            exists = minioClient.bucketExists(BucketExistsArgs.builder().bucket(name).build());
            return Result.ok(exists);
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return Result.ok(false);
    }

    public static File upload(MultipartFile file, String folder) {
        File fileObj = new File();
        try {
            InputStream inputStream = file.getInputStream();
            String fileName = file.getOriginalFilename();
            if (StringUtils.isNotBlank(folder)) {
                fileName = folder + "/" +fileName;
            }
            ObjectWriteResponse response = minioClient.putObject(PutObjectArgs.builder()
                    .bucket(parameter.getBucket())
                    .object(fileName)
                    .stream(inputStream, file.getSize(), -1)
                    .contentType(file.getContentType())
                    .build());
            if (StringUtils.isNotBlank(response.object())) {
                fileObj.setFileName(fileName);
                fileObj.setOssType(parameter.getOssType());
                fileObj.setFolder(folder);
                fileObj.setType(file.getContentType());
                fileObj.setBucket(parameter.getBucket());
                fileObj.setDeleted(1);
                fileObj.setPath(response.object());
                fileObj.setCreateTime(new Timestamp(System.currentTimeMillis()));
                fileObj.setUpdateTime(new Timestamp(System.currentTimeMillis()));

                return fileObj;
            }
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return null;
    }

    /**
     * 获取图片链接
     * @param fileName 文件名
     * @return 链接
     */
    public static String getFileUrl(String fileName) {
        String url = null;
        try {
            url = minioClient.getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder()
                    .method(Method.GET)
                    .bucket(parameter.getBucket())
                    .object(fileName)
                    .build());
            return url;
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return url;
    }
}
